package com.bjesc_b2c.app.appraiset.mvp.presenter.impl;

import android.app.Activity;
import android.content.Context;

import com.bjesc_b2c.app.appraiset.mvp.model.CarSourcePhotoModel;
import com.bjesc_b2c.app.appraiset.mvp.model.impl.CarSourcePhotoModelImpl;
import com.bjesc_b2c.app.appraiset.mvp.presenter.CarSourcePhotoPersnter;
import com.bjesc_b2c.app.appraiset.mvp.view.CarSourcePhotoView;
import com.bjesc_b2c.app.appraiset.utils.TimeListUilts;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.CarPhotoID;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import master_bean.CarSourceImageBean;
import master_bean.CarSourcePhotoBean;

/**
 * Created by lenovo on 2017/3/3.
 */

public class CarSourcePhotoPersenterImpl implements CarSourcePhotoPersnter, CarSourcePhotoModelImpl.CallBack {
    private CarSourcePhotoView view;
    private CarSourcePhotoModel model;
    private List<CarSourceImageBean.ImgInfoBean> imgInfoBeen;  //所有图片集合
    private Context context;

    public CarSourcePhotoPersenterImpl(CarSourcePhotoView view, Context context) {
        this.view = view;
        this.context = context;
        model = new CarSourcePhotoModelImpl(this);
    }

    @Override
    public void getPhoto(String sourceID) {
        model.getPhoto(context, sourceID);
    }

    @Override
    public void callBack(final CarSourceImageBean imageBean, final String sourceID) {
        if (imageBean == null || imageBean.getImg_info() == null) {
            view.getPhoto(null);
            return;
        }
        new Thread() {
            @Override
            public void run() {
                List<CarSourcePhotoBean> list = null;
                final List<String> fileList = new ArrayList<String>();
                String otherId = "";
                switch (sourceID) {
                    case CarPhotoID.SURFACEID:
                        list = TimeListUilts.getSurfacePhotoList();
                        otherId = CarPhotoID.SurfaceID.surfaceOther;
                        break;
                    case CarPhotoID.ORNAMENTSID:
                        list = TimeListUilts.getOrnamentsPhotoList();
                        otherId = CarPhotoID.OrnamentsID.ornamentsOther;
                        break;
                    case CarPhotoID.ENGINEID:
                        list = TimeListUilts.getEnginePhotoList();
                        otherId = CarPhotoID.EngineID.engineOther;
                        break;
                    case CarPhotoID.PROCEDURESID:
                        list = TimeListUilts.getProceduresPhotoList();
                        otherId = CarPhotoID.ProceduresID.proceduresOther;
                        break;
                }
                /**
                 * 循环第一层获取到每一张图片
                 * 循环第二层，获取到整个对象的数据，来判断是否与第一层的某张图片ID对应，如果对应则设置
                 * 循环第三层，当循环到最后一张图片“更多”的时候，将数据“更多”的集合挨个添加到里面
                 * 先取出第一个更多，然后设置属性，再新添一个更多。
                 */
                A:
                for (int i = 0; i < list.size(); i++) {
                    for (int j = 0; j < imageBean.getImg_info().size(); j++) {
                        //将跟多一次性循环到集合中，然后跳出
                        if (otherId.equals(list.get(i).getId()) && otherId.equals(imageBean.getImg_info().get(j).getAttr_id())) {
                            for (int k = 0; k < imageBean.getImg_info().get(j).getImg_name().size(); k++) {
                                CarSourcePhotoBean photoBean = list.get(i);
                                photoBean.setPhoto(imageBean.getImg_info().get(j).getImg_name().get(k));
                                photoBean.setPhotoName("");
                                fileList.add(TimeListUilts.subUrl(imageBean.getImg_info().get(j).getImg_name().get(k))); //将文件名添加到集合，以后需要用来删除的
                                //最后一张更多的ID用-1表示
                                if (k == imageBean.getImg_info().get(j).getImg_name().size() - 1) {
                                    list.add(new CarSourcePhotoBean("-1", "", "更多"));
                                }
                            }
                            break A;
                        } else {//将每张添加到集合中
                            if (list.get(i).getId().equals(imageBean.getImg_info().get(j).getAttr_id()) && imageBean.getImg_info().get(j).getImg_name().size() > 0) {
                                list.get(i).setPhoto(imageBean.getImg_info().get(j).getImg_name().get(0));
                                fileList.add(TimeListUilts.subUrl(imageBean.getImg_info().get(j).getImg_name().get(0))); //将文件名添加到集合，以后需要用来删除的
                            }
                        }
                    }
                }
                final List<CarSourcePhotoBean> finalList = list;
                ((Activity) context).runOnUiThread(new TimerTask() {
                    @Override
                    public void run() {
                        view.getPhoto(finalList);
                        view.getFileName(fileList);
                    }
                });
                ;
            }
        }.start();
    }


    /**
     * @param calssID       //大分类ID
     * @param otherID       //大分类下的更多ID
     * @param photoBeanList //自定义的车源图片的集合
     */
    @Override
    public void savePhoto(String calssID, String otherID, List<CarSourcePhotoBean> photoBeanList) {
        getImgInfoBeen(otherID, photoBeanList);                    //大分类下的更多ID
        model.savePhoto(context, calssID, new Gson().toJson(imgInfoBeen));
        Logger.d("json---=="+new Gson().toJson(imgInfoBeen));

        Logger.d("json---=="+new Gson().toJson(photoBeanList));
    }


    /**
     * 将每个图片对象存到集合
     *
     * @param calssID
     */
    private void getImgInfoBeen(String calssID, List<CarSourcePhotoBean> photoBeanList) {
        imgInfoBeen = new ArrayList<>();
        for (int i = 0; i < photoBeanList.size(); i++) {
            if (photoBeanList.get(i).getId().equals(calssID)) {//将所有更多的图片循环添加到集合
                List<String> urlString = new ArrayList<>();
                for (int j = i; j < photoBeanList.size(); j++) {
                    urlString.add(photoBeanList.get(i).getFileName());
                }
                imgInfoBeen.add(buildUpImageBean(photoBeanList.get(i).getId(), urlString));
                break;
            } else {  //将每一张都添加到集合
                List<String> urlString = new ArrayList<>();
                urlString.add(photoBeanList.get(i).getFileName());
                imgInfoBeen.add(buildUpImageBean(photoBeanList.get(i).getId(), urlString));
            }
        }
    }

    /**
     * 将图片存到对象
     *
     * @param photoId 图片的ID，只是一张
     * @param urlList 图片URL集合，因为考虑到更多是同一个ID，但是有多个图片
     * @return
     */
    private CarSourceImageBean.ImgInfoBean buildUpImageBean(String photoId, List<String> urlList) {
        CarSourceImageBean.ImgInfoBean infoBean = new CarSourceImageBean.ImgInfoBean();
        infoBean.setAttr_id(photoId);
        infoBean.setImg_name(urlList);
        return infoBean;
    }
}
